<template>
  <div class="count">
    <h3>作者是：{{author}}，大写：{{ $store.getters.upperAuthor }}</h3>
    <h3>当前求和为：{{sum}}，放大10被后：{{ $store.getters.bigSum }}</h3>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">当前和为奇数再加</button>
    <button @click="incrementWait">等一等再加</button>
    <button @click="incrementServer">联系服务器再加</button>
  </div>
</template>

<script>
import { mapState } from 'vuex';
  export default {
    name: "Count",
    data() {
      return {
        n: 1,
      };
    },
    computed: {
      // 靠程序员自己去写计算属性，读取state中的数据
      /* zuozhe(){
        return this.$store.state.author
      },
      he(){
        return this.$store.state.sum
      } */

      // 靠mapState生成计算属性，读取state中的数据（第一种：对象写法）
      // ...mapState({author:'author',sum:'sum'})
      
      // 靠mapState生成计算属性，读取state中的数据（第二种：数组写法）
      ...mapState(['author','sum'])
      
    },
    methods: {
      // 点击【+】的回调
      increment() {
        this.$store.commit("JIA", this.n);
      },
      // 点击【-】的回调
      decrement() {
        this.$store.commit("JIAN", this.n);
      },
      // 点击【当前和为奇数再加】的回调
      incrementOdd() {
        this.$store.dispatch("jijia", this.n);
      },
      // 点击【等一等再加】的回调
      incrementWait() {
        this.$store.dispatch("dengjia", this.n);
      },
      incrementServer() {
        this.$store.dispatch("fuwujia");
      }
    },
  }
</script>

<style scoped>
.count {
  background-color: skyblue;
  padding: 10px;
}
button {
  margin-left: 10px;
}
</style>